﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using SimpleOrmCFPC.Core.Query.Requests;
using SimpleOrmCFPC.Core.Query.expressions.RequestBuilder.WhereElmentsBuilder.Helpers;

namespace SimpleOrmCFPC.Core.Query.expressions.ExprssionCompilers.Helpers
{
    public class AnyWhereElementFromExpressionComputer : IWhereElementBuilder
    {
        private Dictionary<Func<Expression, bool>, IWhereElementBuilder> SqlOperatorsMap = new Dictionary<Func<Expression, bool>, IWhereElementBuilder>();

        public void Add(Func<Expression, bool> functor, IWhereElementBuilder computer)
        {
            SqlOperatorsMap.Add(functor, computer);
        }

        public IWhereElement Compute(Expression expression)
        {
            foreach (var map in SqlOperatorsMap.Where(map => map.Key(expression)))
            {
                return map.Value.Compute(expression);
            }
            throw new NotSupportedException("Cannot compile: " + expression.NodeType);
        }

        public void FillAdditionalInformation(Expression expression, SQLRequest request)
        {
            foreach (var map in SqlOperatorsMap.Where(map => map.Key(expression)))
            {
                map.Value.FillAdditionalInformation(expression, request);
                return;
            }
        }
    }
}
